﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PhoneBook_PC2
{
    public partial class ViewEditContact : Form
    {
        PhoneBook.Contact contact;
        Book phoneBook;
        PrincipalFrame principalFrame;

        public ViewEditContact(String firstName, String lastName, Book phoneBook, PrincipalFrame principalFrame)
        {
            InitializeComponent();
            this.CenterToScreen();

            this.phoneBook = phoneBook;
            this.contact = phoneBook.getContact(firstName, lastName);
            this.principalFrame = principalFrame;

            tbFirstName.Text = contact.getPerson().getFirstname();
            tbLastName.Text = contact.getPerson().getLastname();
            tbPhoneCell.Text = contact.getCellnum();
            tbPhoneHome.Text = contact.getHomenum();
            tbPhoneWork.Text = contact.getOfficenum();
            tbURL.Text = contact.getPictureurl();

            try
            {
                Bitmap img = new Bitmap(contact.getPictureurl());
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = (Image)img;
            }
            catch (Exception e)
            {

                Bitmap img = new Bitmap(global::PhoneBook_PC2.Properties.Resources.users);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = (Image)img; 
            }
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "png files (*.png)|*.png|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                String file = openFileDialog1.FileName;
                tbURL.Text = file;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            String firstName = tbFirstName.Text;
            String lastName = tbLastName.Text;
            String phoneHome = tbPhoneHome.Text;
            String phoneWork = tbPhoneWork.Text;
            String phoneCell = tbPhoneCell.Text;
            String url = tbURL.Text;

            if (!this.checkNumbers(phoneCell) || !this.checkNumbers(phoneHome) || !this.checkNumbers(phoneWork))
            {
                Info info = new Info("Bad format of phone number");
                info.Show();
            }

            else
            {
                PhoneBook.Person person = new PhoneBook.Person(firstName, lastName);
                PhoneBook.Contact newContact = new PhoneBook.Contact(person, phoneHome, phoneWork, phoneCell, url);
                phoneBook.modifyContact(contact, newContact);

                this.Close();

                Info info = new Info("Contact modified");
                info.Show();

                List<PhoneBook.Contact> contacts = phoneBook.getContacts();
                List<String[]> rowsList = new List<String[]>();
                foreach (PhoneBook.Contact c in contacts)
                {
                    String firstName2 = c.getPerson().getFirstname();
                    String lastName2 = c.getPerson().getLastname();
                    String cellPhone = c.getCellnum();
                    String homePhone = c.getHomenum();
                    String officePhone = c.getOfficenum();
                    String[] row = { firstName2, lastName2, cellPhone, homePhone, officePhone };
                    rowsList.Add(row);
                }
                principalFrame.populateDataGridView(rowsList);
            }
        }

        public Boolean checkNumbers(String phoneNumber)
        {
            Char[] caracters = phoneNumber.ToCharArray();
            for (int i = 0; i < caracters.Length; i++)
            {
                if (!caracters[i].Equals('0') && !caracters[i].Equals('1') && !caracters[i].Equals('2') && !caracters[i].Equals('3') && !caracters[i].Equals('4') && !caracters[i].Equals('5') && !caracters[i].Equals('6') && !caracters[i].Equals('7') && !caracters[i].Equals('8') && !caracters[i].Equals('9'))
                    return false;
            }
            return true;
        }

        private void ViewEditContact_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                String firstName = tbFirstName.Text;
                String lastName = tbLastName.Text;
                String phoneHome = tbPhoneHome.Text;
                String phoneWork = tbPhoneWork.Text;
                String phoneCell = tbPhoneCell.Text;
                String url = tbURL.Text;

                if (!this.checkNumbers(phoneCell) || !this.checkNumbers(phoneHome) || !this.checkNumbers(phoneWork))
                {
                    Info info = new Info("Bad format of phone number");
                    info.Show();
                }

                else
                {
                    PhoneBook.Person person = new PhoneBook.Person(firstName, lastName);
                    PhoneBook.Contact newContact = new PhoneBook.Contact(person, phoneHome, phoneWork, phoneCell, url);
                    phoneBook.modifyContact(contact, newContact);

                    this.Close();

                    Info info = new Info("Contact modified");
                    info.Show();

                    List<PhoneBook.Contact> contacts = phoneBook.getContacts();
                    List<String[]> rowsList = new List<String[]>();
                    foreach (PhoneBook.Contact c in contacts)
                    {
                        String firstName2 = c.getPerson().getFirstname();
                        String lastName2 = c.getPerson().getLastname();
                        String cellPhone = c.getCellnum();
                        String homePhone = c.getHomenum();
                        String officePhone = c.getOfficenum();
                        String[] row = { firstName2, lastName2, cellPhone, homePhone, officePhone };
                        rowsList.Add(row);
                    }
                    principalFrame.populateDataGridView(rowsList);
                }
            }
        }
    }
}
